package main

import (
	"fmt"
)

type NumMatrix struct {
	preSumMatrix [][]int
}

func Constructor(matrix [][]int) NumMatrix {
	m := len(matrix)
	n := len(matrix[0])
	if m == 0 || n == 0 {
		return NumMatrix{preSumMatrix: [][]int{}}
	}

	preSum := make([][]int, m+1)
	for i := 0; i < m+1; i++ {
		preSum[i] = make([]int, n+1, n+1)
	}

	for i := 1; i <= m; i++ {
		for j := 1; j <= n; j++ {
			preSum[i][j] = matrix[i-1][j-1] + preSum[i-1][j] + preSum[i][j-1] - preSum[i-1][j-1]
		}
	}
	return NumMatrix{preSumMatrix: preSum}
}

func (this *NumMatrix) SumRegion(row1 int, col1 int, row2 int, col2 int) int {
	return this.preSumMatrix[row1][col1] + this.preSumMatrix[row2+1][col2+1] - this.preSumMatrix[row1][col2+1] - this.preSumMatrix[row2+1][col1]
}

/**
 * Your NumMatrix object will be instantiated and called as such:
 * obj := Constructor(matrix);
 * param_1 := obj.SumRegion(row1,col1,row2,col2);
 */
func main() {
	//matrix := [][]int{
	//	{3, 0, 1, 4, 2},
	//	{5, 6, 3, 2, 1},
	//	{1, 2, 0, 1, 5},
	//	{4, 1, 0, 1, 7},
	//	{1, 0, 3, 0, 5},
	//}
	matrix := [][]int{
		{1},
		{-7},
	}
	obj := Constructor(matrix)
	for _, i := range obj.preSumMatrix {
		fmt.Println(i)
	}
	param_1 := obj.SumRegion(0, 0, 0, 0)
	fmt.Println(param_1)
}
